//*****************************************************************************
//
// File Name	: 'ds1302.h'
// Title		: Bitbang driver for Dallas 1302 RTC chip
// Author		: Sami Kukkonen - Copyright (C) 2011
// Created		: 4.7.2011
// Version		: 0.1
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

#ifndef DS1302LIGHT_H
#define DS1302LIGHT_H

// DS1302 port, data direction and pin configuration
#define DS1302_PORT	PORTB
#define DS1302_DDR	DDRB
#define DS1302_PIN	PINB

#define DS1302_RST	PB5	// Reset pin
#define DS1302_SCLK PB7	// Serial clock pin
#define DS1302_IO	PB6	// IO pin

// DS1302 register write addresses
// For register reading: register |= DS1302_READ; (add one)
#define DS1302_R_SECONDS 	0x80
#define DS1302_R_MINUTES 	0x82
#define DS1302_R_HOURS	 	0x84
#define DS1302_R_DAY	 	0x86
#define DS1302_R_MONTH	 	0x88
#define DS1302_R_WEEKDAY 	0x8A
#define DS1302_R_YEAR	 	0x8C
#define DS1302_R_WP		 	0x8E // Write protect
#define DS1302_R_TC			0x90 // Trickle charge
// Burst write addresses
#define DS1302_R_CLKBRST	0xBE
#define DS1302_R_RAMBRST	0xFE
// Ram start and stop addresses (31 bytes total and battery backed)
#define DS1302_R_RAM_START	0xC0
#define DS1302_R_RAM_END	0xFC

#define DS1302_RAM_MAX		31
#define DS1302_RAM_MIN		0

// Bitmasks for flag bits
#define DS1302_WP_SET		0x80 // Write protect (WP register)
#define DS1302_WP_CLEAR		0x00
#define DS1302_HF_SET		0x80 // Halt flag (Seconds register)
#define DS1302_HF_CLEAR		0x00
// Trickle charger commands. D = number of diodes; K = resistance in kohm
#define DS1302_TC_DISABLE	0x00
#define DS1302_TC_E_MASK	0xA0
#define DS1302_TC_1D_2K		0xA5
#define DS1302_TC_1D_4K		0xA6
#define DS1302_TC_1D_8K		0xA7
#define DS1302_TC_2D_2K		0xA9
#define DS1302_TC_2D_4K		0xAA
#define DS1302_TC_2D_8K		0xAB
// 12/24 h flag commands (In hours register)
#define DS1302_12H_MODE		0x80
#define DS1302_24H_MODE		0x00
// AM/PM bit masks (In hours register (only in 12H mode))
#define DS1302_PM_BIT		0x20

// Trickle charger power on setting
#define DS1302_TC_POWER_ON	DS1302_TC_1D_2K

// For customized commandbyte construction
#define DS1302_CMD_START 		0b10000000
#define DS1302_CMD_RAM	 		0b01000000
#define DS1302_CMD_READ 		0b00000001
#define DS1302_RAM_ADR_MASK 	0b00111110



// TODO change this to struct for clarity
enum RTC_INDEX_T {
	SECONDS,
	MINUTES,
	HOURS,
	DAY,
	MONTH,
	WEEKDAY,
	YEAR,
	WPBIT,
	HMODE
};
// ***** Low-level functions *****
// These functions set up the port control registers and access them directly

// Hardware init (DDR & IO). Use ds1302Init() in your code instead.
void ds1302InitHW(void);
// Set IO for input
void ds1302SetIOInput(void);
// Set IO for Output
void ds1302SetIOOutput(void);
// Send commandbyte and prepare for reading a byte after (leaves SCLK high)
void ds1302SendCommand(uint8_t);
// Send a byte
void ds1302SendByte(uint8_t);
// Receive a byte
uint8_t ds1302GetByte(void);
// Write data to register in ds1302 (addr, data)
void ds1302WriteReg(uint8_t, uint8_t);
// Read data from register in ds1302 (addr, data)
uint8_t ds1302ReadReg(uint8_t);

// ***** High-level functions *****
// The following functions provide the high-level control for the RTC like the
// Time set and get functions

// Initialize the IO and send correct WP, HF and TC settings
void ds1302Init(void);

// Sets the time from the rtctable to ds1302 registers
// using burst write mode (rtc table to put data to)
void ds1302GetTime(uint8_t*);
// Gets the time from ds1302 registers to the rtctable
// using burst read mode (rtc table to read from, 12/24 hour mode)
void ds1302SetTime(uint8_t*);

// Utility functions
// Convert 1-byte packed bcd code to hex
unsigned char bcd_to_hex(unsigned char);
//uint8_t bcd_to_hex(uint8_t);
//uint8_t hex_to_bcd(uint8_t);
// Convert hex number to 1-byte packed bcd code
unsigned char hex_to_bcd(unsigned char);

#endif